package com.liping.www.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liping.www.entity.Illness;
import com.liping.www.entity.PageView;
import com.liping.www.mapper.IllnessKindMapper;
import com.liping.www.mapper.IllnessMapper;
import com.liping.www.mapper.PageViewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IllnessService extends ServiceImpl<IllnessMapper, Illness> {
    //注入需要的mapper
    @Autowired
    private IllnessKindMapper illnessKindMapper;
    @Autowired
    private PageViewMapper pageViewMapper;

    public IPage<Illness> getIllnessPageDataByIllnessName(String illnessName, int pageSize, int curPage){
        //每页数据2条，当前第2页
        Page<Illness> illnessPage = new Page<Illness>(curPage,pageSize);
        QueryWrapper<Illness> queryWrapper = new QueryWrapper<>();
        System.out.println(illnessName);
        queryWrapper.like(illnessName!=null,"illness_name",illnessName);
        IPage<Illness> illnessIPage =this.page(illnessPage,queryWrapper);
        //读取关联数据
        //如果有数据
        List<Illness> records = illnessIPage.getRecords();
        if (illnessPage.getRecords().size()>0){
            //遍历过程中，获取关联的属性值
            records.forEach(s->{
                //根据ID读取疾病种类
                s.setKindName(illnessKindMapper.selectById(s.kindId).getName());
                QueryWrapper<PageView> wrapper = new QueryWrapper<>();
                wrapper.eq("illness_id",s.id);
                s.setPageView(pageViewMapper.selectOne(wrapper).getPageViews());
            });

        }
        records.forEach(System.out::println);
        return illnessIPage;
    }
}
